package common.changedtracker;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class FieldChangeTrackerAspect {
	
	@Pointcut("(!cflow(execution(@DisableChangedTracker * *(..))))"
		  + "&&(!cflow(execution(@javax.annotation.PostConstruct * *(..))))")
    public void notInPostConstruct(){
    }

	@Pointcut("(!cflow(initialization(*.new(..))))&&(!cflow(preinitialization(*.new(..))))&&(!cflow(staticinitialization(*)))")
    public void notInInitialization(){
    }
	
	@Pointcut("notInInitialization()&&notInPostConstruct()")
    public void excludePointcut(){
    }
	
	@Pointcut("@annotation(AsChangedOnSetField)&&set(* *.*)&&excludePointcut()")
    public void targetAnnotatedField(){
    }
	
	@Pointcut("@this(AsChangedOnSetAnyField)&&set(* *.*)&&excludePointcut()")
    public void targetAnnotatedClass(){
    }

	@Pointcut("@annotation(AsChangedOnExecute)")
    public void targetAnnotatedMethod(){
    }
	
	@After("(targetAnnotatedField()||targetAnnotatedClass())||targetAnnotatedMethod()")
    public void fieldChangeTracker(JoinPoint joinPoint){
		ChangeMarker.setChanged(joinPoint.getTarget());
    }
}
